/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bishe.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.bishe.entity.BisheAccount;
import com.thinkgem.jeesite.modules.bishe.entity.BishePack;
import com.thinkgem.jeesite.modules.bishe.service.BisheAccountService;
import com.thinkgem.jeesite.modules.bishe.service.BishePackService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bishe.entity.BisheAccountLog;
import com.thinkgem.jeesite.modules.bishe.service.BisheAccountLogService;

/**
 * 资金变动管理Controller
 * @author yue
 * @version 2018-12-28
 */
@Controller
@RequestMapping(value = "${adminPath}/bishe/bisheAccountLog")
public class BisheAccountLogController extends BaseController {

	@Autowired
	private BisheAccountLogService bisheAccountLogService;
	@Autowired
	private BishePackService bishePackService;
	@Autowired
	private BisheAccountService bisheAccountService;
	
	@ModelAttribute
	public BisheAccountLog get(@RequestParam(required=false) String id) {
		BisheAccountLog entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = bisheAccountLogService.get(id);
		}
		if (entity == null){
			entity = new BisheAccountLog();
		}
		return entity;
	}


	@RequestMapping("/jiesuan")
	public String jiesuan(BisheAccountLog bisheAccountLog) {

		try {
			BishePack pack=bishePackService.get(bisheAccountLog.getPack().getId());
			if(pack!=null){
				bisheAccountLog.setAccountMoney(Integer.parseInt(pack.getRentMoney()));
				bisheAccountLog.setOrderType(bisheAccountLog.getOrderType());
				bisheAccountLog.setPack(pack);
				bisheAccountLogService.save(bisheAccountLog);
				BisheAccount acc=bisheAccountService.getOne();
				if(acc==null){
					acc=new BisheAccount();
					acc.setAccountMoney(bisheAccountLog.getAccountMoney());

				}else {
					acc.setAccountMoney(acc.getAccountMoney()+bisheAccountLog.getAccountMoney());
				}
				bisheAccountService.save(acc);
				pack.setJiesuanFlag(1);
				bishePackService.save(pack);
			}
		}catch (Exception e){

		}
		return "redirect:../bishePack";

	}

	@RequiresPermissions("bishe:bisheAccountLog:view")
	@RequestMapping(value = {"list", ""})
	public String list(BisheAccountLog bisheAccountLog, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<BisheAccountLog> page = bisheAccountLogService.findPage(new Page<BisheAccountLog>(request, response), bisheAccountLog); 
		model.addAttribute("page", page);
		return "modules/bishe/bisheAccountLogList";
	}

	@RequiresPermissions("bishe:bisheAccountLog:view")
	@RequestMapping(value = "form")
	public String form(BisheAccountLog bisheAccountLog, Model model) {
		model.addAttribute("bisheAccountLog", bisheAccountLog);
		return "modules/bishe/bisheAccountLogForm";
	}

	@RequiresPermissions("bishe:bisheAccountLog:edit")
	@RequestMapping(value = "save")
	public String save(BisheAccountLog bisheAccountLog, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, bisheAccountLog)){
			return form(bisheAccountLog, model);
		}
		bisheAccountLogService.save(bisheAccountLog);
		addMessage(redirectAttributes, "保存资金变动信息成功");
		return "redirect:"+Global.getAdminPath()+"/bishe/bisheAccountLog/?repage";
	}
	
	@RequiresPermissions("bishe:bisheAccountLog:edit")
	@RequestMapping(value = "delete")
	public String delete(BisheAccountLog bisheAccountLog, RedirectAttributes redirectAttributes) {
		bisheAccountLogService.delete(bisheAccountLog);
		addMessage(redirectAttributes, "删除资金变动信息成功");
		return "redirect:"+Global.getAdminPath()+"/bishe/bisheAccountLog/?repage";
	}

}